/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 字符串.String类.string与Byte数组;

import org.junit.Test;

import java.io.UnsupportedEncodingException;

public class StringAndByte {
    public static void main(String[] args) {

    }
    @Test
    public void test01(){
        String s1 = "abc中国";
        byte[] b1 = s1.getBytes();// 使用默认字符集进行转换 “编码”
        for (byte i : b1) {// 输出ASCII值 “解码”
            System.out.print(i+" ");
        }
        System.out.println();
        byte[] b2 = new byte[0];
        try {
            b2 = s1.getBytes("gbk");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        for (byte i : b2) {// 输出ASCII值
            System.out.print(i+" ");
        }
        System.out.println();

        String s3 = new String(b1);// 使用默认字符集解码

        String s4 = null;
        try {
            s4 = new String(b2,"gbk");// 使用指定字符集解码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        System.out.println(s3);// abc中国
        System.out.println(s4);// 未使用原指定字符集解码 “abc�й�” // 更改后恢复正常

    }
}
